package com.lczyfz.istep.modules.openstack.api.nova.impl.serveraction;

import com.lczyfz.istep.modules.openstack.api.nova.impl.parseJson.NovaParse;
import com.lczyfz.istep.modules.openstack.api.nova.interfaces.serveraction.ServerActions;
import com.lczyfz.istep.modules.openstack.api.nova.model.Server;
import com.lczyfz.istep.modules.openstack.api.nova.model.serveraction.RebuildServer;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

public class ServerActionsImpl implements ServerActions {
	
public RequestFactory factory = new RequestFactory("nova");
	
	@Override
	public void LockServer(String serverid) {
		JSONObject params = NovaParse.packageLockServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
	}

	@Override
	public void PauseServer(String serverid) {
		JSONObject params = NovaParse.packagePauseServer();
			System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}

	@Override
	public void RebootServer(String serverid) {
		JSONObject params = NovaParse.packageRebootServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}

	@Override
	public Server RebuildServer(String serverid, RebuildServer rebuildserver) {
			Server server = new Server();
		JSONObject params = NovaParse.packageRebuildServer(rebuildserver);
			System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
			JSONObject res = results.getResponseBody();
			server = NovaParse.resolveServerDetail(res,null);
			return server;
			
	}

	@Override
	public void ResizeServer(String serverid, String flavorRef) {
		JSONObject params = NovaParse.packageResizeServer(flavorRef);
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}

	@Override
	public void ResumeSuspendedServer(String serverid) {
		JSONObject params = NovaParse.packageResumeSuspendedServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}

	@Override
	public void RevertResizedServer(String serverid) {
		JSONObject params = NovaParse.packageRevertResizedServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}

	@Override
	public void StartServer(String serverid) {
		JSONObject params = NovaParse.packageStartServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}

	@Override
	public void StopServer(String serverid) {
		JSONObject params = NovaParse.packageStopServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}

	@Override
	public void SuspendServer(String serverid) {
		JSONObject params = NovaParse.packageSuspendServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}

	@Override
	public void UnlockServer(String serverid) {
		JSONObject params = NovaParse.packageUnlockServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
		
	}

	@Override
	public void UnpauseServer(String serverid) {
		JSONObject params = NovaParse.packageUnpauseServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
		
	}

	@Override
	public void ForceDeleteServer(String serverid) {
		JSONObject params = NovaParse.packageForceDeleteServer();
		//	System.out.println(params.toString());
			Request request = factory.getRequest();
			request.setUrl("/servers/"+serverid + "/action");
			request.setRequestType(RequestType.POST);
			request.setBody(params);
			Response results = HTTP.getConnectionResult(request);	
			
			if( ConstParams.RESPONSE_CODE_DELETE!= results.getResponseCode()){
				throw new RuntimeException("Failed : error code : " + results.getResponseCode());
			}
		
	}
	
	public static void main(String[] args){

		ServerActionsImpl ServerActionsdemo = new ServerActionsImpl();
	//	System.out.println("����������");
	//	ServerActionsdemo.LockServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("����������");
	//	ServerActionsdemo.UnlockServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("��ͣ������");
	//	ServerActionsdemo.PauseServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("ȡ����ͣ������");
	//	ServerActionsdemo.UnpauseServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("������������С");
	//	ServerActionsdemo.ResizeServer("34225da0-4709-4ae2-bb89-b15dab58c609", "84");
		
	//	System.out.println("��ԭ������С�ķ�����");
	//	ServerActionsdemo.RevertResizedServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("����������");
	//	ServerActionsdemo.StartServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("ֹͣ������");
	//	ServerActionsdemo.StopServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("���������");
	//	ServerActionsdemo.SuspendServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("�ָ�����ķ�����");
	//	ServerActionsdemo.ResumeSuspendedServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("��������������");
	//	ServerActionsdemo.RebootServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
	//	System.out.println("�ؽ�������");
	//	RebuildServer rs = new RebuildServer();
	//	rs.setImageRef("575f2ce6-c5c5-487a-9751-ae141c820a8a");
	//	rs.setName("testvm");
	//	Server server = ServerActionsdemo.RebuildServer("34225da0-4709-4ae2-bb89-b15dab58c609", rs);
	//	System.out.println(server.getName()+" "+server.getId());
	//	System.out.println("ǿ��ɾ��������");
	//	ServerActionsdemo.ForceDeleteServer("34225da0-4709-4ae2-bb89-b15dab58c609");
		
		
	}
}
